<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>迭代器</title>
</head>
<body>
    <script>
        let names = ["cq","cqcq","cqqqq"];
        let nameIterator = function(arr)
        {
            let index = 0;
            return {
                next:function()
                {
                    if(index<arr.length)
                    {
                        return {done:false,value:arr[index++]};
                    }
                    else
                    {
                       return {done:true};
                    }
                }
            }
        }
        // let NameIterator = nameIterator(names);
        // console.log(NameIterator.next().value);
        // console.log(NameIterator.next().value);
        // console.log(NameIterator.next().value);
        // console.log(NameIterator?.next()?.value);

        //可迭代对象内部有一个属性symbol.iterator
        console.log(names[Symbol.iterator]);
        
        //实现可迭代对象

        let infos = {
            friend:"cq",
            friends:["james","kobe","curry"],
            [Symbol.iterator]:function()
            {
                let index = 0;
                const iterator = 
                {
                    next:function()
                    {
                        if(index>=infos.friends.length)
                        {
                            return {done:true};
                        }
                        else
                        {
                            return {done:false,value:infos.friends[index++]}
                        }
                    }
                }
                return iterator;
            }
        }

        const infoIterator = infos[Symbol.iterator]();
        console.log(infoIterator.next().value);
        console.log(infoIterator.next().value);
        console.log(infoIterator.next().value);

        for(val of infos)
        {
            console.log(val);
        }
       
    </script>
</body>
</html>